package com.fzkj.shejimoshi.strategy1.annotation;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @DESCRIPTION: TODO  用于解析注解
 * @AUTHOR: AnotherOne
 * @DATE: 2021/7/17 22:50
 */
public class AnnotationHandler {

    /** file类型 */
    private static final String FILE = "file";
    /** jar类型 */
    private static final String JAR = "jar";

    /**
     * 获取类上的注解
     * @param clazz 类对象
     */
    public static void parseAnnotation(Class<?> clazz){
        // 是Register修饰的
        if (clazz.isAnnotationPresent(Register.class)){
            // 获取注解
            Register register = clazz.getAnnotation(Register.class);
            System.out.println(register);
            System.out.println(register.name());
            String name = register.name();

        }
    }

    /**
     * 获取类上注解，类路径下所有
     */
    public static void parseAnnotation(){
        String path = "com.fzkj.shejimoshi.strategy1";
        getClass(path);
    }

    /**
     * 通过路径名获取下面所有class
     * @param path
     * @return
     */
    public static Set<Class<?>> getClass(String path){
        Set<Class<?>> clazzs = new LinkedHashSet<>();

        String packageName = path.replaceAll("\\.", "/");
//        packageName = "com/fzkj/shejimoshi/strategy1";
        Enumeration<URL> dirs;
        try {
            dirs = Thread.currentThread().getContextClassLoader().getResources(packageName);
            // 只能获取到一个
            while (dirs.hasMoreElements()){
                URL url = dirs.nextElement();
                String protocol = url.getProtocol();
                System.out.println("协议：" + protocol);

            }
            System.out.println("===============");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        parseAnnotation();
//        try {
//            Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources("com/fzkj/shejimoshi/strategy1");
//
//            while (resources.hasMoreElements()){
//                System.out.println("lala");
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }

}
